﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetWikiBot;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace FlowgraphViewer.Parsers
{
    public class CryWikiParser
    {
        public static void Parse()
        {
            Site wiki = new Site("http://wiki.crymod.com", "login", "pass");

            wiki.site = "http://wiki.crymod.com";



            PageList pl = new PageList(wiki);

            pl.FillAllFromCategory("Flowgraph_Node_Reference");

            using (StreamWriter wr = new StreamWriter("CrysisNodes.cs"))
            {
                wr.WriteLine("public System.Collections.Generic.IEnumerable<FlowNode> RegisterNodes()");
                wr.WriteLine("{");
                wr.WriteLine("List<FlowNode> toReturn = new List<FlowNode>();");
                wr.WriteLine();
                wr.WriteLine("FlowNode node;");

                wr.WriteLine();
                wr.WriteLine();

                foreach (Page p in pl.pages)
                {
                    p.Load();
                    ParsePage(wr, p.title, p.text);
                }

                wr.WriteLine("return toReturn;");
                wr.WriteLine("}");

            }
        }

        private static void ParsePage(StreamWriter wr, string title, string text)
        {
            string name = Regex.Match(text, @"\|name=([^\n]+)").Groups[1].Value;
            string category = Regex.Match(text, @"\|category=([^\n]+)").Groups[1].Value;
            string description = Regex.Match(text, @"\|description=([^\n]*)").Groups[1].Value;

            Debug.WriteLine(name);

            string inputs = Regex.Match(text, @"\|inputs=([^$]+)\|outputs=", RegexOptions.Multiline | RegexOptions.IgnoreCase).Groups[1].Value;

            wr.WriteLine("node = new FlowNode(EFlowNodeCategory.{0}, \"{1}\", \"{2}\");",
                category,
                title.Substring(7), // remove FGNode:
                description);

            MatchCollection inputMatches = Regex.Matches(inputs, @"{{FGPortTemplate\|name=(.+)\|humanname=(.+)\|type=(.+)\|description=(.+)\|defaultvalue=(.*)}}");

            wr.WriteLine();

            foreach (Match m in inputMatches)
            {
                wr.WriteLine("node.Inputs.Add(new FlowNodeItem {{ Name = \"{0}\", TechnicalName=\"{4}\", Description = \"{1}\", Type = EFlowNodeItemType.{2}, Value={3} }});",
                    m.Groups[2].Value,
                    m.Groups[4].Value,
                    m.Groups[3].Value,
                    (m.Groups.Count < 6 || String.IsNullOrWhiteSpace(m.Groups[5].Value) || m.Groups[5].Value.Equals("None")) ? "null" : (m.Groups[3].Value.ToLower().Equals("vector") ? '"' + m.Groups[5].Value + '"' : m.Groups[5].Value),
                    m.Groups[1].Value
                    );
            }


            string outputs = Regex.Match(text, @"\|outputs=([^$]+)\|notes=", RegexOptions.Multiline | RegexOptions.IgnoreCase).Groups[1].Value;
            MatchCollection outputMatches = Regex.Matches(outputs, @"{{FGPortTemplate\|name=(.+)\|humanname=(.+)\|type=(.+)\|description=(.+)\|defaultvalue=(.+)}}");

            wr.WriteLine();

            foreach (Match m in outputMatches)
            {
                wr.WriteLine("node.Outputs.Add(new FlowNodeItem {{ Name = \"{0}\", TechnicalName=\"{4}\", Description = \"{1}\", Type = EFlowNodeItemType.{2}, Value={3} }});",
                    m.Groups[2].Value,
                    m.Groups[4].Value,
                    m.Groups[3].Value,
                    m.Groups[5].Value.Equals("None") ? "null" : m.Groups[5].Value,
                    m.Groups[1].Value
                    );
            }
            wr.WriteLine("toReturn.Add(node);");


            wr.WriteLine();
            wr.WriteLine();
        }
    }
}
